package leetcodecompetition.D361;

import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-09-03-11:23
 * @Description: 1.0
 */
public class T6952 {
    public long countInterestingSubarrays(List<Integer> nums, int modulo, int k) {
        int n = nums.size();
        long[] ls = new long[n + 1];
        long r = 0;
        for (int i = 0; i < n; i++) {
            if (nums.get(i) % modulo == k) r++;
            ls[i + 1] = r;
        }
        int left = 0, right = 0;
        long res = 0;
        while (right < n + 1) {
            while (right < n + 1 && ls[right] - ls[left] < k) {
                right++;
            }
            int nestRight = right;
            while (nestRight < n + 1 && ls[nestRight] - ls[left] == k) {
                nestRight++;
            }
            int nextLeft = left;
            while (nextLeft < n + 1 && ls[right] - ls[nextLeft] == k) {
                nextLeft++;
            }
            res = (long) (nestRight - right) * (nextLeft - left) * (ls[left] % modulo);
            right=nestRight;
            left=nextLeft;
            System.out.println(left+" "+right);
        }
        return res;
    }
}
